package com.xxgc.vfc.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author:sjy
 * @Date :2023/10/31 - 10 - 31 - 16:43
 * 时间过滤器，当前请求的耗时
 * 自定义名称 + Gateway + Filter
 * 局部过滤器
 */
public class TimeGatewayFilter implements GatewayFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //当前时间
        long startTime = System.currentTimeMillis();
        //chain.filter(exchange) 放行
        //Mono.fromRunnable 响应（异步回调）
        return chain.filter(exchange).then(Mono.fromRunnable(()->{
            //请求结束
            long stopTime = System.currentTimeMillis();
            //请求结束时间-开始时间
            long time = stopTime - startTime;
            System.out.println("当前请求耗时"+time);
        }));
    }
}
